home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / Graphic Elements 2 / GEDemo / Cannon.c next >
Encoding:
C/C++ Source or Header  |  1994-06-24  |  5.2 KB  |  213 lines  |  [TEXT/MPS ]

  1. /*
  2.     Cannon.c
  3.     
  4.     Cannon scene for GEDemo
  5.     
  6.     Copyright 1993 by Al Evans. All rights reserved.
  7.     
  8.     11/3/93
  9.     
  10. */
  11.  
  12. #include "Cannon.h"
  13. #include "Motion.h"
  14. #include "SFXCtrlr.h"
  15. #include "SFXProcs.h"
  16.  
  17. #undef COMMERCIALRELEASE
  18.  
  19.  
  20. static MotionParams    ballMotion;
  21.  
  22. #ifdef COMMERCIALRELEASE
  23. pascal void DisposeMask(GEWorldPtr world, GrafElPtr element)
  24. {
  25.     if (element->drawData)
  26.         DisposPtr(element->drawData);
  27. }
  28. #endif
  29.  
  30. Boolean LoadCannonScene(GEWorldPtr world)
  31. {
  32.     GrafElPtr        cannon, thisElement;
  33.     short            smokeWidth, smokeHeight;
  34.  
  35.     //Get cannon picture
  36.     cannon = NewBasicPICT(world, cannonID, cannonPlane, rCannonPic, 
  37.                                     transparent, cannonLeft, cannonTop);
  38.     if (cannon == nil) return false;
  39.     
  40.     //Get cannonball picture
  41.     thisElement = NewBasicPICT(world, ballID, ballPlane, rBallPic,
  42.                                     transparent, cannonLeft - 8, cannonTop - 8);
  43.     if (thisElement == nil) return false;
  44.     
  45. #ifdef COMMERCIALRELEASE
  46.     //Draw cannonball masked
  47.     thisElement->drawData = MakeMask(&((GrafPtr) thisElement->graphWorld)->portBits);
  48.     thisElement->copyMode = srcCopy;
  49.     
  50.     //Be sure to Dispose Mask
  51.     SetCleanupProc(world, ballID, DisposeMask);
  52. #endif
  53.  
  54.     //Initialize cannonball's motion
  55.     SetAutoChange(world, ballID, DoCannonBall, (Ptr) &ballMotion, 33);
  56.     
  57.     //Initialize motion parameters
  58.     ballMotion.limitRect = world->animationRect;
  59.     RectOffset(&ballMotion.limitRect, 0, ScaleToWorld(world,-80));
  60.     InitMotion(&ballMotion, 2, 90);
  61.     
  62.     //Initialize cannonball's collision params
  63.     SetCollision(world, ballID, DoBallHit, 650);
  64.     
  65.     //Cannonball is initially invisible
  66.     ShowElement(world, ballID, false);
  67.     
  68.     //Get animated smoke picture
  69.     thisElement = NewAnimatedGraphic(world, smokeID, smokePlane, rSmokePic,
  70.                                     transparent, 0, 0, 3);
  71.     if (thisElement == nil) return false;
  72.     
  73.     //Position smoke in relation to cannon
  74.     smokeWidth = RectWidth(&thisElement->graphRect);
  75.     smokeHeight = RectHeight(&thisElement->graphRect);
  76.     PtrMoveElementTo(world, thisElement, ScaleToWorld(world, cannonLeft + 16) - smokeWidth, 
  77.                                     ScaleToWorld(world, cannonTop + 16) - smokeHeight, false);
  78.     
  79.     //Set smoke animation
  80.     AnimateGraphic(world, smokeID, 170, oneshot);
  81.     
  82.     //Smoke is initially invisible
  83.     ShowElement(world, smokeID, false);
  84.     
  85.     //Smoke is attached to cannon
  86.     cannon->slaveGrafEl = thisElement;
  87.     
  88.     //Get "FIRE" button
  89.     thisElement = NewButtonSensor(world, fBtnID, btnPlane, rFBtnPic, 434, 193);
  90.     if (thisElement == nil) return false;
  91.     SetSensorAction(world, fBtnID, ShootCannon);
  92.     
  93.     return true;
  94. }
  95.  
  96. pascal void DoCannonBall(GEWorldPtr world, GrafElPtr ball)
  97. {
  98.     MParamPtr    motion;
  99.     
  100.     motion = (MParamPtr) ball->changeData;
  101.     
  102.     if ((motion->currMotion.v == 0) && (motion->currMotion.h == 0)) {
  103.         //ShowElement(world, ball->objectID, false);
  104.         (void) DoGESFX(world, 'SFXB', ball, SFXVWipe,
  105.                         RectHeight(&ball->animationRect), 0, 200, false, true);
  106.         return;
  107.     }
  108.     MoveElement(world, ball->objectID, motion->currMotion.h, motion->currMotion.v);
  109.     switch (CheckLimits(&ball->animationRect, &motion->limitRect)) {
  110.     case down:
  111.         if (motion->currMotion.v > 0) {
  112.             //DoSound(cannonBall.sound);
  113.             DoBounce(v, motion);
  114.         }
  115.         DoFriction(motion);
  116.         break;
  117.     case right:
  118.         if (motion->currMotion.h > 0) {
  119.             DoBounce(h, motion);
  120.             //DoSound(cannonBall.sound);
  121.         }
  122.         break;
  123.     case left:
  124.         if (motion->currMotion.h < 0) {
  125.             DoBounce(h, motion);
  126.             //DoSound(cannonBall.sound);
  127.         }
  128.         break;
  129.     default:
  130.         motion->currMotion.v++;
  131.         break;
  132.     }
  133. }
  134.  
  135. pascal void DoBallHit(GEWorldPtr world, GrafElPtr ball, 
  136.                     GEDirection dir, CollisionPhase phase, GrafElPtr objHit)
  137. {
  138.  
  139.     MParamPtr    motion;
  140.     
  141.     if (phase == collisionBegin) {
  142.         motion = (MParamPtr) ball->changeData;
  143.         switch (dir) {
  144.             case left:
  145.                 if (motion->currMotion.h < 0)
  146.                     DoBounce(h, motion);
  147.                 break;
  148.             case right:
  149.                 if (motion->currMotion.h > 0)
  150.                     DoBounce(h, motion);
  151.                 break;
  152.             case up:
  153.                 if (motion->currMotion.v < 0)
  154.                     DoBounce(v, motion);
  155.                 break;
  156.             case down:
  157.                 if (motion->currMotion.v > 0)
  158.                     DoBounce(v, motion);
  159.                 break;
  160.             case upLeft:
  161.                 if (motion->currMotion.v < 0)
  162.                     DoBounce(v, motion);
  163.                 if (motion->currMotion.h < 0)
  164.                     DoBounce(h, motion);
  165.                 break;
  166.             case downLeft:
  167.                 if (motion->currMotion.v > 0)
  168.                     DoBounce(v, motion);
  169.                 if (motion->currMotion.h < 0)
  170.                     DoBounce(h, motion);
  171.                 break;
  172.             case upRight:
  173.                 if (motion->currMotion.v < 0)
  174.                     DoBounce(v, motion);
  175.                 if (motion->currMotion.h > 0)
  176.                     DoBounce(h, motion);
  177.                 break;
  178.             case downRight:
  179.                 if (motion->currMotion.v > 0)
  180.                     DoBounce(v, motion);
  181.                 if (motion->currMotion.h > 0)
  182.                     DoBounce(h, motion);
  183.                 break;
  184.             
  185.         }
  186. //        if (objHit->objectID != 'SFXP')        //Just say no to recursive effects
  187.         if (FindElementByID(world, 'SFXP') == nil)    //One at a time, please
  188.             (void) DoGESFX(world, 'SFXP', objHit, SFXBlink, 10, 0, 50, true, true);
  189.     }
  190. }
  191.  
  192. pascal void ShootCannon(GEWorldPtr world, short fireIt)
  193. {
  194.     GrafElPtr cannon, ball;
  195. #pragma unused (fireIt)
  196.  
  197.     cannon = FindElementByID(world, cannonID);
  198.     if (cannon) {
  199.         ballMotion.currMotion.h = -20;
  200.         ballMotion.currMotion.v = -20;
  201.         ballMotion.frictAcc = 0;
  202.         ball = FindElementByID(world, ballID);
  203.         if (ball) {
  204.             PtrMoveElementTo(world, ball, cannon->animationRect.left - ScaleToWorld(world, 8), 
  205.                     cannon->animationRect.top - ScaleToWorld(world, 8), false);
  206.             ShowElement(world, ballID, true);
  207.         }
  208.         //DoSound(blam);
  209.         ShowElement(world, smokeID, true);
  210.     }
  211. }
  212.  
  213.